package cn.cocho.android.mc.storage;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

import cn.cocho.android.mc.character.StringUtils;

/**
 * 操作SharedPreference
 *
 * @author HAIKANG SONG
 * @time 2013-5-13上午11:43:20
 */
public class SharedPreferenceUtils {

    private static SharedPreferences sharePreference;


    /**
     * 将键值对设置到内存中
     *
     * @param key   键
     * @param value 值（支持String，Integer，Long，Float，Boolean）
     * @return true:成功，false:失败
     * @author shk
     */
    public static boolean setMemory(String key, Object value) {
        boolean bool = false;
        getSharePreference();
        Editor editor = sharePreference.edit();
        try {
            if (key != null && value != null) {
                if (value instanceof String) {
                    editor.putString(key, value.toString());
                } else if (value instanceof Integer) {
                    editor.putInt(key, Integer.parseInt(value.toString()));
                } else if (value instanceof Long) {
                    editor.putLong(key, Long.parseLong(value.toString()));
                } else if (value instanceof Float) {
                    editor.putFloat(key, Float.parseFloat(value.toString()));
                } else if (value instanceof Boolean) {
                    editor.putBoolean(key, Boolean.parseBoolean(value.toString()));
                } else {
                    return bool;
                }
            }
            editor.commit();
            bool = true;
        } catch (Exception e) {
            Log.e(SharedPreferenceUtils.class.toString(), "将键[" + key + "]值[" + value + "]保存到内存卡中出错！", e);
        }
        return bool;
    }

    /**
     * 根据键从内存中取值
     * @param key       键
     * @param defValue  默认值，不能为null
     * @param type 返回值类型（支持String，Integer，Long，Float，Boolean）
     * @param <T>
     * @return 值/null
     */
    @SuppressWarnings("unchecked")
    public static <T> T getMemory(String key, Object defValue, Class<T> type) {
        Object obj = null;
        try {
            if (StringUtils.isNotEmpty(key)) {
                getSharePreference();
                if (String.class.equals(type)) {
                    obj = sharePreference.getString(key, defValue.toString());
                } else if (Integer.class.equals(type)) {
                    obj = sharePreference.getInt(key, Integer.parseInt(defValue.toString()));
                } else if (Long.class.equals(type)) {
                    obj = sharePreference.getLong(key, Long.parseLong(defValue.toString()));
                } else if (Float.class.equals(type)) {
                    obj = sharePreference.getFloat(key, Float.parseFloat(defValue.toString()));
                } else if (Boolean.class.equals(type)) {
                    obj = sharePreference.getBoolean(key, Boolean.parseBoolean(defValue.toString()));
                } else {
                    throw new RuntimeException("返回值类型仅支持String，Integer，Long，Float，Boolean");
                }
            }

            if (obj != null) {
                return (T) obj;
            }
        } catch (Exception e) {
            Log.e(SharedPreferenceUtils.class.toString(), "根据键[" + key + "]从内存卡中取值出错！", e);
        }
        return null;
    }

    /**
     * 获得sharePreference对象
     *
     * @return sharePreference对象
     * @author HAIKANG SONG
     * @time 2013-5-13上午11:25:20
     */
    public static SharedPreferences getSharePreference() {
        if (sharePreference == null) {
            sharePreference = PreferenceManager.getDefaultSharedPreferences(ApplicationCache.context);
        }
        return sharePreference;
    }

    public static void setSharePreference(SharedPreferences sharePreference) {
        SharedPreferenceUtils.sharePreference = sharePreference;
    }


    /**
     * 删除一个SharePreference中的值
     *
     * @param key
     * @return void
     * @author hsx
     * @time 2013-6-17下午04:24:54
     */
    public static void deleteSharePreferenceValue(String key) {
        getSharePreference();
        Editor editor = sharePreference.edit();
        editor.remove(key);
        editor.commit();
    }


}
